import { useWebConfigStore,useStatusStore,useRefs } from "@/stores";
class Control {
    context: uni.InnerAudioContext;
    GLOBALSTATUS:any;
    constructor(src:string,GLOBALSTATUS:any) {
        this.GLOBALSTATUS=GLOBALSTATUS;
        if(src){
            this.context=uni.createInnerAudioContext();
            this.context.src=src;
            this.context.autoplay=false; 
            this.context.loop=false;
            this.context.obeyMuteSwitch=false;
            this.context.volume=1;
            this.context.startTime=0;
        }
    }
    play() {
        if(this.GLOBALSTATUS.value?.sound){
            return;
        }
        this.context?.play();
    }
    pause() {
        this.context?.pause();
    }
    stop() {
        this.context?.stop();
    }
    seek(position:number) {
        this.context?.seek(position);
    }
    destroy() {
        this.context?.destroy();
    }
	rest(){
        this.pause();
        this.seek(0);
    }
	restPlay(){
        this.seek(0);
        this.play();
    }
}
export function createAudio(name: string) {
    const StatusStore=useStatusStore();
    const webConfigStore = useWebConfigStore();
    const { WEBCONFIG } = useRefs(webConfigStore);
    const {GLOBALSTATUS}=useRefs(StatusStore);
    const src=WEBCONFIG.value?.audio?.[name];
    const control=new Control(src,GLOBALSTATUS);
    return control;
}